import { NextRequest } from 'next/server';
import { withMiddleware } from '@/lib/middleware/entry';
import { successResponse, errorResponse } from '@/lib/response';
import { prisma } from '@/lib/prisma';
import { authenticateRequest } from '@/utils/auth';

async function handler(req: NextRequest) {
  const authResult = await authenticateRequest(req);
  if (!authResult.success) {
    return errorResponse(authResult.error || '身份验证失败');
  }

  const { name, path, type } = (await req.json()) || {};

  const menus = await prisma.menu.findMany({
    where: {
      name: {
        contains: name,
      },
      path: {
        contains: path,
      },
      ...(type && { type }),
    },
    include: {
      categories: true
    },
    orderBy: {
      order: 'asc',
    },
  });

  return successResponse(menus);
}

export const POST = withMiddleware(handler);